import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {

        Scanner sc = new Scanner(System.in);
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();
        System.out.println("请输入修改信息人的ID：");
        int id = sc.nextInt();
        System.out.println("请输入修改姓名：");
        String name = sc.next();
        String sql = "update student set name = ? where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setInt(2,id);
        System.out.println("preparedStatement:"+preparedStatement);
        int ret = preparedStatement.executeUpdate();
        System.out.println(ret);

        preparedStatement.close();
        connection.close();
        sc.close();
    }
}
